package com.info.zhiduoduo.repository.mapper.order;

import com.baomidou.mybatisplus.mapper.BaseMapper;
import com.info.zhiduoduo.repository.dto.account.TradeRecordBriefDto;

import com.info.zhiduoduo.repository.entity.order.TradeRecord;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 第三方支付交易记录 Mapper 接口
 * </p>
 *
 * @author bob
 * @since 2019-01-16
 */
public interface TradeRecordMapper extends BaseMapper<TradeRecord> {

	@Select("select tradeid from zdd_trade_record where repay = 0 and tradestate <= 2")
	List<Integer> getInProcessTradeids();

	@Select("SELECT tr.* from zdd_trade_record tr " +
			" JOIN zdd_payment pay on tr.pay_id = pay.id" +
			" where pay.source_table = 1 " +
			" and tr.tradestate = 3 " +
			" and tr.createtime > DATE_SUB(NOW(),INTERVAL 2 DAY)")
	List<TradeRecord> getSuccessTradeIds();

	@Select(" select tr.tradeid,tr.tradeno,tr.idtype,tr.projectid,tr.tradeamount,tr.tradetype,tr.pay_id,pay.pay_name, " +
			" tr.tradestate,tr.repay,tr.createtime,tr.purpose,tr.partner_order_no,tr.tradetime, " +
			" tr.errorcode,tr.errormsg,tr.userid,IF(tr.idtype=1,us.username,c.compname) as username,c2.compid,c2.compname,tr.adminid " +
			" from zdd_trade_record tr " +
			" left join zdd_maker_studio us on us.userid = tr.userid " +
			" left join zdd_company c on c.compid = tr.userid " +
			" left join zdd_company_project cp on cp.projectid = tr.projectid " +
			" left join zdd_company c2 on c2.compid = cp.compid " +
			" left join zdd_payment pay on pay.id = tr.pay_id " +
			" where if(#{tradetype}=0,1=1,tr.tradetype = #{tradetype}) " +
			" and if(#{tradestate}=0,1=1,tr.tradestate=#{tradestate}) " +
			" and tr.createtime between #{startDate} and #{endDate} " +
			" and (if(tr.idtype=1,us.username like '%${name}%',c.compname like '%${name}%') or c2.compname like '%${name}%' )" +
			" order by " +
			" ( CASE WHEN tr.tradestate = 4 AND tr.repay = 0 THEN 99 ELSE 1 END ) DESC, " +
			" ( CASE WHEN tr.tradestate = 5 THEN 99 ELSE 1 END ) DESC, " +
			" ( CASE WHEN tr.tradestate = 2 THEN 99 ELSE 1 END ) DESC, " +
			" tr.createtime DESC")
	List<TradeRecordBriefDto> getTradeRecordList(@Param("name") String name, @Param("tradetype") int tradetype, @Param("tradestate") int tradestate,
	                                             @Param("startDate") Date startDate, @Param("endDate") Date endDate);

	@Select(" select tr.tradeid,tr.tradeno,tr.idtype,tr.projectid,tr.tradeamount,tr.tradetype,tr.pay_id," +
			" tr.tradestate,tr.repay,tr.createtime,tr.purpose,tr.partner_order_no,tr.tradetime, " +
			" tr.errorcode,tr.errormsg,tr.userid,IF(tr.idtype=1,us.username,c.compname) as username,c2.compid,c2.compname,tr.adminid " +
			" from zdd_trade_record tr " +
			" left join zdd_maker_studio us on us.userid = tr.userid " +
			" left join zdd_company c on c.compid = tr.userid " +
			" left join zdd_company_project cp on cp.projectid = tr.projectid " +
			" left join zdd_company c2 on c2.compid = cp.compid " +
			" where if(#{tradetype}=0,1=1,tr.tradetype = #{tradetype}) " +
			" and if(#{tradestate}=0,1=1,tr.tradestate=#{tradestate}) " +
			" and tr.createtime between #{startDate} and #{endDate} " +
			" and (if(tr.idtype=1,us.username like '%${name}%',c.compname like '%${name}%') or c2.compname like '%${name}%' )" +
			" and tr.repay = 0 "  +
			" order by " +
			" ( CASE WHEN tr.tradestate = 4 AND tr.repay = 0 THEN 99 ELSE 1 END ) DESC, " +
			" tr.createtime DESC")
	List<TradeRecordBriefDto> TradeRecordList(@Param("name") String name, @Param("tradetype") int tradetype, @Param("tradestate") int tradestate,
											  @Param("startDate") Date startDate, @Param("endDate") Date endDate);
}
